package com.youi.web;

import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.common.DelStatus;
import com.youi.service.MerchantclassService;
import com.youi.entity.Merchantclass;
import com.youi.common.Common;
import com.youi.util.CommonInfoUtil;
import com.youi.util.DateUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;

/**
* 商户分类控制层
* @author Administrator
*/
@Controller
public class MerchantclassController  extends HttpRequestManage{

		@Autowired
		private MerchantclassService merchantclassService;

		/**
		 * I-11
		   *  添加或编辑商户分类
		*/
		@ResponseBody
		@RequestMapping("sysUserGetInfo/addOrEditMerchantclass")
		public String addOrEditMerchantclass(ModelMap modelMap,HttpServletRequest request)throws BusinessException,ParseException{
			Long id=optLong("id",request);
			Merchantclass merchantclass=null;
			if (id!=null) {
				merchantclass=merchantclassService.get(id);
			}else {
				merchantclass=new Merchantclass();
				merchantclass.setDelStatus(DelStatus.NODEL);
				merchantclass.setGmtCreated(new Date());
			}
			String name=optString("name",request);
			if (name!=null){
				merchantclass.setName(name);
			}
			String img=optString("img",request);
			if (img!=null){
				merchantclass.setImg(img);
			}
			Integer isUse=optInteger("isUse",request);
			if (isUse!=null){
				merchantclass.setIsUse(isUse);
			}
			merchantclass.setGmtModify(new Date());
			merchantclassService.saveOrUpdate(merchantclass);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-14
		 *  批量操作商户分类
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/operateMerchantclassByIds")
		public String delMerchantclassByIds(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			String ids=getString("ids",request);
			Integer type=getInteger(Common.TYPE, request);
			Integer isUse=optInteger("isUse", request);
			List<Long> idsList=ObjectUtil.getIdListByIds(ids);
			if (idsList==null||idsList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
			}
			Map<String, Object> params=new HashMap<String, Object>();
			params.put(Common.DELSTATUS, DelStatus.NODEL);
			params.put(Common.ID, idsList);
			List<Merchantclass> merchantclassList= merchantclassService.getByParams(params);
			if (merchantclassList==null||merchantclassList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			for (Merchantclass merchantclass : merchantclassList) {
				switch (type) {
				case 0:
					merchantclass.setDelStatus(DelStatus.DEL);
					break;
				case 1:
					merchantclass.setIsUse(isUse);
					break;
				}
			}
			merchantclassService.saveOrUpdateAll(merchantclassList);
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
		}

		/**
		 * I-12 
		 * 查询商户分类列表
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getMerchantclass")
		public String getMerchantclass(ModelMap modelMap,HttpServletRequest request) throws BusinessException,ParseException{
			Integer pageIndex=optInteger("pageIndex",request,1);
			Integer pageNum=optInteger("pageNum",request,10);
			Integer isUse=optInteger("isUse", request);
			String des=optString("des",request,Common.DESC);
			String pms=optString("pms",request);
			Date start=optDate("start",request,"yyyy-MM-dd HH:mm:ss");
			Date end=optDate("end",request,"yyyy-MM-dd HH:mm:ss");
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			if (isUse!=null) {
				prams.put("isUse", isUse);
			}
			Map<String, Object> sortPram=new HashMap<String, Object>();
			sortPram.put(Common.ID,des);
			Map<String, Object> searchPram=null;
			if (pms!=null) {
				searchPram=new HashMap<String, Object>();
				searchPram.put("name", pms);
			}
			Map<String, Object> startTime=null;
			if(start!=null){
				startTime=new HashMap<String, Object>();
				startTime.put(Common.GMTCREATED,start);
			}
			Map<String, Object> endTime=null;
			if(end!=null){
				endTime=new HashMap<String, Object>();
				endTime.put(Common.GMTCREATED,end);
			}
			List<Merchantclass> merchantclassList=merchantclassService.getByParamsList(prams, searchPram,sortPram,  (pageIndex-1)*pageNum, pageNum, startTime, endTime);
			if (merchantclassList==null||merchantclassList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			Integer pageCount=merchantclassService.getCountByParamss(prams, searchPram, startTime, endTime);
			modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
			modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);			//pageIndex     查询页数
			modelMap.put(CommonInfoUtil.ALLCOUINT,pageCount);
			modelMap.put(CommonInfoUtil.DATA, merchantclassList);		//jsonObjectList json对象集合
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",modelMap);
		}

		/**
		 * I-13
		   *    查询商户分类详情 
		 * @param modelMap
		 * @param request
		 * @return
		 * @throws BusinessException
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getMerchantclassById")
		public String getMerchantclassById(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Long id=getLong("id",request);
			Merchantclass merchantclass=merchantclassService.get(id);
			if (merchantclass==null) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",merchantclass);//jsonObject	json对象
		}

		/**
		 * I-15
		 * 查询所有商户分类
		 * @param modelMap
		 * @param session
		 * @return
		 */
		@ResponseBody
		@RequestMapping("sysUserGetInfo/getAllMerchantclass")
		public String getAllMerchantclass(ModelMap modelMap,HttpServletRequest request) throws BusinessException{
			Map<String, Object> prams=new HashMap<String, Object>();
			prams.put(Common.DELSTATUS, DelStatus.NODEL);
			prams.put("isUse", 1);
			List<Merchantclass> merchantclassList= merchantclassService.getByParams(prams);
			if (merchantclassList==null||merchantclassList.size()==0) {
				return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
			}
			return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成",merchantclassList);//jsonObjectList	json对象集合
		}
}
